package com.crm.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.easyui.Grid;
import com.crm.po.Config;
import com.crm.po.InventoryPp;
import com.crm.service.InventoryPpService;
import com.crm.util.ResponseUtils;

public class InventoryPpAction extends Servlet {

	private static final long serialVersionUID = 1116994082953057010L;
	
	@SuppressWarnings("unchecked")
	public void getInventoryPpList(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();
		Map<String, Object> map = service.getInventoryPps(request);
		
		Grid grid = new Grid();
		grid.setTotal((Integer)map.get("count"));
		grid.setRows((List<InventoryPp>)map.get("rows"));
		
		ResponseUtils.printJson(response, null, grid, null,null);
	}
	
	public void saveOrUpdateInventoryPp(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		boolean result = service.saveOrUpdateInventoryPp(request);
		
		String value = result ? "ok": "新增或修改数据失败，请检查您的输入数据是否有效。";
		ResponseUtils.printString(response, value);
	}
	
	public void updateDdnumber(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		boolean result = service.updateDdnumber(request);
		
		String value = result ? "ok": "修改数据失败，请检查您的输入数据是否有效。";
		ResponseUtils.printString(response, value);
	}
	
	public void deleteInventoryPp(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		boolean result = service.deleteInventoryPps(request);
		
		String value = result ? "ok": "删除数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void recoverInventoryPp(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		boolean result = service.recoverInventoryPps(request);
		
		String value = result ? "ok": "恢复数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void reallyDeleteInventoryPp(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		boolean result = service.reallyDeleteInventoryPps(request);
		
		String value = result ? "ok": "彻底删除数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void getConfig(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		Config config = service.getConfig(request);
	
		ResponseUtils.printJson(response, null, config, null,null);
	}
	
	public void exeSystemConfig(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		boolean result = service.exeSystemConfig(request);
	
		String value = result ? "ok": "系统配置失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void mathJJCC(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		double result = service.mathJJCC(request);
		
		String temp = result + "";
		String returnValue = temp;
		String[] args = temp.split("\\.");
		if(temp.indexOf(".") < 0){
			returnValue = returnValue + ".00";
		}else if(args[1] == null || args[1].length() == 0){
			returnValue = returnValue + "00";
		}else if(args[1].length() == 1){
			returnValue = returnValue + "0";
		}
		
		ResponseUtils.printString(response, returnValue);
	}
	
	public void mathBatchAdd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();

		String result = service.mathBatchAdd(request);

		ResponseUtils.printJson(response, result);
	}
	
	public void exportInventoryPpExcel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InventoryPpService service = new InventoryPpService();
		service.exportInventoryPps(request, response);
	}

}
